﻿using System.Collections.Generic;
using itextsharp.pdfa;
using iTextSharp.text;
using iTextSharp;
using iTextSharp.text.pdf;
using System.IO;
using System.Web;
using System;
namespace VCC.Lib.Common
{
    public static class FileBuilding
    {
        public static string CreatePDF(string savePath, string fileName, string content)
        {
            string mPath = string.Format("{0:yyyy/MM/dd}", DateTime.Now);
            mPath = mPath.Replace("/", "\\");
            savePath = savePath + mPath + "\\";
            //Tao thu muc chua file
            if (!Directory.Exists(savePath))
            {
                // If it doesn't then we just create it before going any further
                Directory.CreateDirectory(savePath);
            }
            string filePath = savePath + fileName;
            Document doc = new Document(PageSize.A4, 2, 2, 2, 2);
            // Create paragraph for show in PDF file header
            Paragraph p = new Paragraph(content);
            //p.
            //p.SetAlignment("center"); 

            try
            {
                PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
                //Create table here for write database data
                //PdfPTable pdfTab = new PdfPTable(4); // here 4 is no of column
                //pdfTab.HorizontalAlignment = 1; // 0- Left, 1- Center, 2- right
                //pdfTab.SpacingBefore = 20f;
                //pdfTab.SpacingAfter = 20f;
                doc.Open();
                doc.Add(p);
                //doc.Add(pdfTab);
                doc.Close();
                

                //ChoVietDataContext eDataContact = new ChoVietDataContext();
                //List<ContactUs> data = eDataContact.ContactUs.Where(e => e.ID <= 5).ToList();

                //pdfTab.AddCell("Sl. No.");
                //pdfTab.AddCell("Name");
                //pdfTab.AddCell("PhongeNumber");
                //pdfTab.AddCell("Email");

                //foreach (var item in data)
                //{
                //    pdfTab.AddCell(item.ID.ToString());
                //    pdfTab.AddCell(item.Name);
                //    pdfTab.AddCell(item.PhoneNumber);
                //    pdfTab.AddCell(item.Email);
                //}
                //doc.Open();
                //doc.Add(p);
                //doc.Add(pdfTab);
                //doc.Close();

                //byte[] content = System.IO.File.ReadAllBytes(filePath);
                //HttpContext context = System.Web.HttpContext.Current;
                ////HttpContext context = HttpContext.Current;

                //context.Response.BinaryWrite(content);
                //context.Response.ContentType = "application/pdf";
                //context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                //context.Response.End();
            }
            catch (Exception exp)
            {
                throw;
            }
            finally
            {
                doc.Close();
            }
            return filePath;
        }
    }
}
